package fr.cephb.socialsnp.hapmap;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.util.Set;
import java.util.TreeSet;
import java.util.zip.GZIPInputStream;

import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.DatabaseNotFoundException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;

public class BuildHapMap {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			
			File envDir=new File("/tmp/HapmapDB");
			PrintWriter out= new PrintWriter(new File(envDir,"allrs.txt"));
			EnvironmentConfig envConfig = new EnvironmentConfig();
			envConfig.setAllowCreate(true);
			
			Environment hapmapEnv  = new Environment(envDir, envConfig);
			
			
			
			URL base= new URL("http://ftp.hapmap.org/genotypes/latest/rs_strand/non-redundant/");
			BufferedReader r= new BufferedReader(new InputStreamReader(base.openStream()));
			String line;
			Set<String> files= new TreeSet<String>();
			while((line=r.readLine())!=null)
				{
				String tokens[]=line.split("[\\\"]");
				for(String s:tokens)
					{
					if(s.startsWith("genotypes_") && s.endsWith(".txt.gz"))
						{
						files.add(s);
						}
					}
				}
			r.close();
			
			try { hapmapEnv.removeDatabase(null, "Populations"); } catch(DatabaseNotFoundException err) { }
			
			
			DatabaseConfig dbcfg = new DatabaseConfig();
			dbcfg.setSortedDuplicates(false);
			dbcfg.setAllowCreate(true);
			Database popDB= hapmapEnv.openDatabase(null, 
                    "Populations", 
                    dbcfg
                    ); 
			
			
			
			
		    for(String pop:new String[]{"CEU"})
			    {
		    	try {hapmapEnv.removeDatabase(null,pop); } catch(DatabaseNotFoundException err) { }
		    	Database genotypesDB= hapmapEnv.openDatabase(null, 
		    			pop, 
	                    dbcfg
	                    ); 
		    	
				String prevheader=null;
				for(String file:files)
					{
					if(!file.contains(pop)) continue;
					
					URL url=new URL(base,file);
					r= new BufferedReader(new InputStreamReader(new GZIPInputStream(url.openStream())));
					line= r.readLine();
					if(line==null) throw new IOException("No first line");
					if(prevheader!=null && !prevheader.equals(line)) throw new IOException("not the same header");
					if(prevheader==null)
						{
						popDB.put(null,
							new DatabaseEntry(pop.getBytes("UTF-8")),
							new DatabaseEntry(line.getBytes("UTF-8"))
							);
						}
					prevheader=line;
					
					while((line=r.readLine())!=null)
						{
						int i=line.indexOf(' ');
						if(i==-1) throw new IOException("blank missing");
						String rs= line.substring(0,i).trim().toLowerCase();
						if(!rs.startsWith("rs")) throw new IOException("not a rs "+rs);
						
						genotypesDB.put(null,
								new DatabaseEntry(rs.getBytes("UTF-8")),
								new DatabaseEntry(line.getBytes("UTF-8"))
								);
						out.println(rs);
						}
					
					r.close();
					break;
					}
				genotypesDB.close();
			    }
			out.close();
			popDB.close();
			hapmapEnv.compress();
			hapmapEnv.cleanLog();
			hapmapEnv.close();
			} 
		catch (Exception e)
			{
			e.printStackTrace();
			}

	}

}
